package com.gzhu.knowledgeAdmin.parammanage;

import com.gzhu.knowledgeAdmin.common.dto.param.ParamAddRequest;
import com.gzhu.knowledgeAdmin.common.dto.param.ParamDeleteRequest;
import com.gzhu.knowledgeAdmin.common.dto.param.ParamGetRequest;
import com.gzhu.knowledgeAdmin.common.vo.ParamVO;
import com.gzhu.knowledgeAdmin.entity.Category;
import com.gzhu.knowledgeAdmin.repository.CategoryRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryParamCRUD implements ParamCRUD<ParamVO>{

    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public List<String> getSimpleList() {
//        List<Recorder> recorderList = recorderRepository.findAll();
//        return recorderList.stream().map(recorder -> recorder.getName()).collect(Collectors.toList());
        return null;
    }

    @Override
    public Page<ParamVO> getList(ParamGetRequest paramGetRequest) {
        Category category = new Category();
        category.setCategory(paramGetRequest.getCategory());
        category.setValue(paramGetRequest.getValue());

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("category",ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("value",ExampleMatcher.GenericPropertyMatchers.contains());

        Example<Category> example = Example.of(category, matcher);

        Pageable pageable = PageRequest.of(paramGetRequest.getPageNum() - 1, paramGetRequest.getPageSize());
        Page<Category> categoryPage = categoryRepository.findAll(example, pageable);
        List<ParamVO> paramVOList = categoryPage.get().map(categoryElem -> {
            ParamVO paramVO = new ParamVO();
            paramVO.setId(categoryElem.getId());
            paramVO.setName(categoryElem.getValue());
            return paramVO;
        }).collect(Collectors.toList());

        PageImpl<ParamVO> res = new PageImpl<>(paramVOList,pageable,categoryPage.getTotalElements());

        return res;
    }

    @Override
    public void addParam(ParamAddRequest paramAddRequest) {
        if (StringUtils.isBlank(paramAddRequest.getCategory())) {
            //为空,说明添加分类(一级菜单)
            Category category = new Category();
            category.setCategory("");
            category.setValue(paramAddRequest.getValue());
            categoryRepository.save(category);
        } else {
            //不为空,添加二级菜单
            Category category = new Category();
            category.setCategory(paramAddRequest.getCategory());
            category.setValue(paramAddRequest.getValue());
            categoryRepository.save(category);
        }
    }

    @Override
    public void deleteParam(ParamDeleteRequest paramDeleteRequest) {
        List<Integer> ids = paramDeleteRequest.getIds();
        if (StringUtils.isBlank(paramDeleteRequest.getCategory())) {
            //为空,说明删除分类(一级菜单)，要删除其下的所有应用场景
            //获取对应的category名称
            List<Category> categoryList = categoryRepository.getCategoryByIdIn(ids);
            List<String> categoryNameList = categoryList.stream().map(Category::getValue).collect(Collectors.toList());
            //删除这些分类名称下的应用场景
            categoryRepository.deleteCategoryByCategoryIn(categoryNameList);
            //删除这些分类
            categoryRepository.deleteAllByIdInBatch(ids);
        } else {
            //不为空,则删除二级菜单
            categoryRepository.deleteAllByIdInBatch(ids);
        }
    }
}
